liwe.form5.plugin_add ( 'fmupload', function ( frm, type, name, attrs )
{
	var f = new liwe.form5.instance ( frm.name + "-fmupload" );

	if ( ! attrs [ 'module' ] ) console.warn ( "FORM5/fmupload: no 'module' defined in attrs" );

	f.add ( "file", "files", { multiple: !! attrs [ 'multiple' ], label: attrs [ 'label' ] } );
	f.add ( "progress", "prog", { form: true } );
	f.add ( "hidden", "module", { value: attrs [ 'module' ] } );
	f.add ( "hidden", "id_obj", { value: 0 } );

	f.action = "file_manager.ajax.file_add";

	f.addEventListener ( "complete", function ()
	{
		console.debug ( "UPLOAD COMPLETE: %o", arguments );
		attrs [ 'complete' ] && attrs [ 'complete' ] ();
	} );

	frm.addEventListener ( "complete", function ( ev ) 
	{ 
		var id_obj = ev.params.response [ 'id_obj' ];

		if ( ! id_obj ) console.error ( "FORM5/fmupload: no 'id_obj' returned from submit." );

		f.set_value ( "id_obj", id_obj );
		f.dispatchEvent ( "submit" );

		return false;
	} );

	f.templates [ 'form-start' ] = '';
	f.templates [ 'form-end' ]   = '';

	attrs [ '_fmupload' ] = f;

	attrs [ 'get_value' ] = function () { return null; };
	attrs [ 'set_value' ] = function () {};

	attrs [ 'render' ] = function ( form, item )
	{
		return f.to_string ();
	};
} );

liwe.form5.templates [ 'fmbrowse' ] = '<button onclick="file_manager.browse(\'%(name)s\')">&hellip;</button>';

liwe.form5.plugin_add ( 'fmbrowse', function ( frm, type, name, attrs )
{
	attrs [ 'render' ] = function ( form, item )
	{
		return String.formatDict ( frm.templates [ 'fmbrowse' ], { name: name } );
	};
} );
